package com.enqualcomm.kids.network;


import com.android.volley.ParseError;
import com.enqualcomm.kids.config.StateCode;


/**
 * Created by yinzhiqun on 2015/9/14.
 * 对响应体进行解析
 */
public class EntityDecoder {

    public int stateCode;
    public String json;
    public byte[] file;

    public EntityDecoder decode(byte[] data) throws ParseError {
//        StringBuilder builder = new StringBuilder();
//        for (int i = 0; i<data.length;i++) {
//
//            builder.append(data[i]).append("^");
//            if (i!=0&&i%24==0){
//                builder.append("\n");
//            }
//        }
//        MyLogger.jLog().i(builder.toString());

        decodeStateCode(data);
        if (stateCode == StateCode.OK){
            int jsonLength = decodeJson(data);
            decodeFile(data,jsonLength);
        }
        return this;
    }

    /**
     * 解析状态码
     */
    private void decodeStateCode(byte[] data) throws ParseError {
        if (data.length<4){
            throw new ParseError();
        }else {
            stateCode = (data[0] & 0xff) << 24
                    |   (data[1] & 0xff) << 16
                    |   (data[2] & 0xff) <<  8
                    |   (data[3] & 0xff);
        }
    }

    /**
     * 解析json字符串
     * @return json字符串长度
     */
    private int decodeJson(byte[] data) throws ParseError {
        if (data.length < 8){
            throw new ParseError();
        }else{
            int jsonLength = (data[4] & 0xff) << 24
                    |   (data[5] & 0xff) << 16
                    |   (data[6] & 0xff) <<  8
                    |   (data[7] & 0xff);

            if (jsonLength != 0){//返回内容有json字符串
                try {
                    byte[] jsonPart = new byte[jsonLength];
                    System.arraycopy(data,8,jsonPart,0,jsonLength);
                    json = new String(jsonPart);
                } catch (Exception e) {
                    throw new ParseError(e);//数组越界或者jsonLength太大，小于0
                }
            }
            return jsonLength;
        }
    }

    /**
     * 解析文件
     * @param jsonLength json字符串长度
     */
    private void decodeFile(byte[] data, int jsonLength) throws ParseError {
        if (data.length < 16 +jsonLength){
            throw new ParseError();
        }else{
            int fileLength = (data[8+jsonLength] & 0xff) << 56
                    |   (data[9+jsonLength] & 0xff) << 48
                    |   (data[10+jsonLength] & 0xff) <<  40
                    |   (data[11+jsonLength] & 0xff) << 32
                    |   (data[12+jsonLength] & 0xff) << 24
                    |   (data[13+jsonLength] & 0xff) << 16
                    |   (data[14+jsonLength] & 0xff) <<  8
                    |   (data[15+jsonLength] & 0xff);

            if (fileLength!=0){
                try {
                    file = new byte[fileLength];
                    System.arraycopy(data,13+jsonLength,file,0,fileLength);
                } catch (Exception e) {
                    throw new ParseError(e);//数组越界或者fileLength太大，小于0
                }
            }

        }
    }


}
